/**
 * 
 */
package sar

/**
 * @author win_64
 *
 */
class TeachingReportController {
	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def springSecurityService

	def index() {
		if(params.fromDate&&params.toDate){
			def tpil=TeachingSession.findAllByTimeBetween(params.fromDate, params.toDate)

			return	[teachingReportInstanceList: tpil, teachingReportInstanceTotal: tpil.size()]
		}
		[teachingReportInstanceList: new ArrayList<TeachingSession>(), teachingReportInstanceTotal: 0]
	}
	
	def studentAttendantReport() {
		def principal = springSecurityService.principal
		
		if(params.programCourse && params.fromDate && params.toDate) {
			def programCourse = ProgramCourse.findByProgramAndStartDateBetweenAndEndDateBetween(Program.get(params.programCourse.id.toLong()), params.fromDate, params.toDate, params.fromDate, params.toDate)
			
			def studentCourse = StudentCourse.findByProgramCourseAndStudent(programCourse, Student.findByUser(User.get(principal.id.toLong())));
			
			def _studentAttendantInstanceList= StudentAttendant.findAllByStudentCourse(studentCourse)

			return	[studentAttendantInstanceList: _studentAttendantInstanceList, studentAttendantInstanceTotal: _studentAttendantInstanceList.size()]
		}
		[studentAttendantInstanceList: new ArrayList<StudentAttendant>(), studentAttendantInstanceTotal: 0]
	}
	
	def tutorCheckAttendantReport() {
		def programCourses = sar.ProgramCourse.list()
		List students = new ArrayList()
		if (programCourses.size() > 0 && params.programCourse == null) {
			def programCourse = ProgramCourse.findByProgram(programCourses.get(0).program)
			def _studentCourse = StudentCourse.findAllByProgramCourse(programCourse);

			for (int i = 0; i < _studentCourse.size(); i++) {
				students.add(_studentCourse.get(i).student)
			}
		} else {
			def programCourse = ProgramCourse.findByProgram(Program.get(params.programCourse.id.toLong()))
			def _studentCourse = StudentCourse.findAllByProgramCourse(programCourse);

			for (int i = 0; i < _studentCourse.size(); i++) {
				students.add(_studentCourse.get(i).student)
			}
		}
		
		if(params.programCourse && params.fromDate && params.toDate) {
			
			def studentCourse = StudentCourse.findByProgramCourseAndStudent(ProgramCourse.get(params.programCourse.id.toLong()), Student.get(params.student.id.toLong()));
			def _studentAttendantInstanceList= StudentAttendant.findAllByStudentCourse(studentCourse)
			return	[studentAttendantInstanceList: _studentAttendantInstanceList, studentAttendantInstanceTotal: _studentAttendantInstanceList.size(), students : students]
		}
		[studentAttendantInstanceList: new ArrayList<StudentAttendant>(), studentAttendantInstanceTotal: 0, students : students]
	}
}

